<template>
  <div style="padding: 20px">
    <el-tabs v-model="activeName" type="border-card" @tab-click="handleClick">
      <el-tab-pane label="包装材料" name="first">
        <div>
          <el-form
            style="width: 100%; display: flex; justify-content: space-between"
            ref="queryForm"
            :model="queryForm"
            :inline="true"
            label-width="68px"
          >
            <div>
              <el-form-item label="">
                <el-input
                  v-model="queryForm.name"
                  placeholder="请输入材料名称"
                  style="width: 400px"
                  clearable
                ></el-input>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="small" @click="handleQuery"
                  >查询
                </el-button>
              </el-form-item>
            </div>
            <div style="float: right">
              <el-form-item>
                <el-button type="primary" icon="el-icon-plus" @click="addType"
                  >添加包装材料</el-button
                >
              </el-form-item>
            </div>
          </el-form>
        </div>
        <div>
          <el-table :data="data" size="medium" border>
            <el-table-column
              label="包材名称"
              align="center"
              prop="packing_material_name"
            ></el-table-column>
            <el-table-column
              label="适用包材类型"
              align="center"
              prop="resource"
            >
              <template slot-scope="scope">
                <div v-if="scope.row.packing_material_type.length==1">
                  <span v-if="scope.row.packing_material_type[0]=='1'">直接接触包材</span>
                  <span v-if="scope.row.packing_material_type[0]=='2'">非直接接触包材</span>
                </div>
                <div v-if="scope.row.packing_material_type.length==2">
                  <span>直接接触包材&nbsp;&nbsp;&nbsp;&nbsp;</span>
                  <span>非直接接触包材</span>
                </div>
              </template>
            </el-table-column>
            <el-table-column label="操作" 
            align="left"
            header-align="center"
            width="180">
              <template slot-scope="scope">
                <el-button type="text"  size="meduim" @click="editType(scope.row)"
                  >编辑
                </el-button>
                <el-button
                  class="text-btn-red"
                  type="text"
                  size="meduim"
                  @click="deleteItem(scope.row, scope.$index)"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
          <pagination style="display:flex;justify-content:flex-end;padding-bottom:0px!important" :total="total"
          :page.sync="pages.pageNum" :limit.sync="pages.pageSize" @pagination="afreshList" />
        </div>
      </el-tab-pane>
      <el-tab-pane label="封口形式" name="second">
        <div>
          <el-form
            style="width: 100%; display: flex; justify-content: space-between"
            ref="queryForm"
            :model="queryForm"
            :inline="true"
            label-width="68px"
          >
            <div>
              <el-form-item label="">
                <el-input
                  v-model="queryForm.name1"
                  placeholder="请输入封口形式名称"
                  style="width: 400px"
                  clearable
                ></el-input>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="small" @click="handleQuery1"
                  >查询
                </el-button>
              </el-form-item>
            </div>
            <div style="float: right">
              <el-form-item>
                <el-button type="primary" icon="el-icon-plus" @click="addType1"
                  >添加封口形式</el-button
                >
              </el-form-item>
            </div>
          </el-form>
        </div>
        <div>
          <el-table :data="data1" size="medium" border>
            <el-table-column
              label="封口形式名称"
              align="center"
              prop="sealing_name"
            ></el-table-column>
            <el-table-column
              label="适用包材类型"
              align="center"
              prop="resource"
            >
              <template slot-scope="scope">
                <div v-if="scope.row.sealing_type.length==1">
                  <span v-if="scope.row.sealing_type[0]=='1'">直接接触包材</span>
                  <span v-if="scope.row.sealing_type[0]=='2'">非直接接触包材</span>
                </div>
                <div v-if="scope.row.sealing_type.length==2">
                  <span>直接接触包材&nbsp;&nbsp;&nbsp;&nbsp;</span>
                  <span>非直接接触包材</span>
                </div>
                <!-- <div>
                  <span v-if="scope.row.sealing_type[0]">内包装材料&nbsp;&nbsp;&nbsp;&nbsp;</span>
                  <span v-if="scope.row.sealing_type[1]">外包装材料</span>
                </div> -->
              </template>
            </el-table-column>
            <el-table-column label="操作" 
            align="left"
            header-align="center"
            width="180">
              <template slot-scope="scope">
                <el-button type="text"  size="meduim" @click="editType1(scope.row)"
                  >编辑
                </el-button>
                <el-button
                  class="text-btn-red"
                  type="text"
                  size="meduim"
                  @click="deleteItem1(scope.row, scope.$index)"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
          <pagination style="display:flex;justify-content:flex-end;padding-bottom:0px!important" :total="total1"
          :page.sync="pages1.pageNum" :limit.sync="pages1.pageSize" @pagination="afreshList1" />
        </div>
      </el-tab-pane>
      <el-tab-pane label="辅助措施" name="third">
        <div>
          <el-form
            style="width: 100%; display: flex; justify-content: space-between"
            ref="queryForm"
            :model="queryForm"
            :inline="true"
            label-width="68px"
          >
            <div>
              <el-form-item label="">
                <el-input
                  v-model="queryForm.name2"
                  placeholder="请输入辅助措施名称"
                  style="width: 400px"
                  clearable
                ></el-input>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="small" @click="handleQuery2"
                  >查询
                </el-button>
              </el-form-item>
            </div>
            <div style="float: right">
              <el-form-item>
                <el-button type="primary" icon="el-icon-plus" @click="addType2"
                  >添加辅助措施</el-button
                >
              </el-form-item>
            </div>
          </el-form>
        </div>
        <el-table :data="data2" size="medium" border>
          <el-table-column
            prop="secondary_measure_name"
            label="辅助措施名称"
            align="center"
          >
          </el-table-column>
          <el-table-column label="操作" 
            align="left"
            header-align="center"
            width="180">
            <template slot-scope="scope">
              <el-button type="text"  size="meduim" @click="editType2(scope.row)"
                  >编辑
              </el-button>
              <el-button
                class="text-btn-red"
                type="text"
                size="meduim"
                @click="deleteItem2(scope.row, scope.$index)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <pagination style="display:flex;justify-content:flex-end;padding-bottom:0px!important" :total="total2"
          :page.sync="pages2.pageNum" :limit.sync="pages2.pageSize" @pagination="afreshList2" />
      </el-tab-pane>
    </el-tabs>
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="600px"
      append-to-body
      :destroy-on-close="true"
      :before-close="handleClose"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="包装材料名称" prop='type'>
          <el-input v-model="form.type" placeholder="请输入包装材料名称"/>
        </el-form-item>
        <el-form-item label="适用包材类型" prop='sType'>
          <el-checkbox-group v-model="checkList">
                <el-checkbox label="1">直接接触包材</el-checkbox>
                <el-checkbox label="2">非直接接触包材</el-checkbox>
          </el-checkbox-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button plain @click="handleClose">取消</el-button>
        <el-button plain @click="cSubmitForm" v-if="title=='添加包装材料'">保存并继续添加</el-button>
        <el-button type="primary" @click="submitForm">保存</el-button>
      </div>
    </el-dialog>
    <el-dialog
      :title="title1"
      :visible.sync="open1"
      width="600px"
      append-to-body
      :destroy-on-close="true"
      :before-close="handleClose1"
    >
      <el-form ref="form1" :model="form1" :rules="rules1" label-width="110px">
        <el-form-item label="封口形式名称" prop="type1">
          <el-input v-model="form1.type1" placeholder="请输入封口形式名称" />
        </el-form-item>
        <el-form-item label="适用包材类型" prop="sType1">
          <el-checkbox-group v-model="checkList1">
                <el-checkbox label="1">直接接触包材</el-checkbox>
                <el-checkbox label="2">非直接接触包材</el-checkbox>
          </el-checkbox-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button plain @click="handleClose1">取消</el-button>
        <el-button plain  @click="cSubmitForm1" v-if="title1=='添加封口形式'">保存并继续添加</el-button>
        <el-button type="primary" @click="submitForm1">保存</el-button>
      </div>
    </el-dialog>
    <el-dialog
      :title="title2"
      :visible.sync="open2"
      width="600px"
      append-to-body
      :destroy-on-close="true"
      :before-close="handleClose2"
    >
      <el-form ref="form2" :model="form2" :rules="rules2" label-width="110px">
        <el-form-item label="辅助措施名称" prop="type2">
           <el-input v-model="form2.type2" placeholder="请输入辅助措施名称"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button plain @click="handleClose2">取消</el-button>
        <el-button plain @click="cSubmitForm2" v-if="title2=='添加辅助措施'">保存并继续添加</el-button>
        <el-button type="primary" @click="submitForm2">确定</el-button>
      </div>
    </el-dialog>      
  </div>
</template>

<script>
import {
  materialList,
  materialCreate,
  materialUpdate,
  materialDelete,
  sealingList,
  sealingCreate,
  sealingUpdate,
  sealingDelete,
  secondaryList,
  secondaryCreate,
  secondaryUpdate,
  secondaryDelete
  } from "@/api/stability";
export default {
  data() {
    let type = (rule, value, callback) => {
      if (this.form.type != '') {
        if(this.form.type.split(" ").join("").length == 0){
          callback(new Error("包装材料名称不能全是空格"));
        }else{
          callback();
        }
      } else {
        callback(new Error("包装材料名称不能为空"));
      }
    };
    let type1 = (rule, value, callback) => {
      if (this.form1.type1 != '') {
        if(this.form1.type1.split(" ").join("").length == 0){
          callback(new Error("封口形式名称不能全是空格"));
        }else{
          callback();
        }
      } else {
        callback(new Error("封口形式名称不能为空"));
      }
    };
    let type2 = (rule, value, callback) => {
      if (this.form2.type2 != '') {
        if(this.form2.type2.split(" ").join("").length == 0){
          callback(new Error("辅助措施名称不能全是空格"));
        }else{
          callback();
        }
      } else {
        callback(new Error("辅助措施名称不能为空"));
      }
    };
    let sType = (rule, value, callback) => {
      if (this.checkList.length > 0) {
        callback();
      } else {
        callback(new Error("请至少选择一个包材类型"));
      }
    };
    let sType1 = (rule, value, callback) => {
      if (this.checkList1.length > 0) {
        callback();
      } else {
        callback(new Error("请至少选择一个包材类型"));
      }
    };
    return {
      title:'添加包装材料',
      title1:'添加封口形式',
      title2:'添加辅助措施',
      checkList:[],
      checkList1:[],
      pages:{
        pageNum:1,
        pageSize:10

      },
      pages1:{
        pageNum:1,
        pageSize:10

      },
      pages2:{
        pageNum:1,
        pageSize:10

      },
      total:0,
      total1:0,
      total2:0,
      activeName: "first",
      queryForm: {
        type: "",
        name: "",
        name1:'',
        name2:'',
      },
      current: null,
      isEdit: false,
      values: "",
      current: null,
      data: [
      
      ],
      data1: [
      
      ],
      data2: [
      
      ],
      open: false,
      open1: false,
      open2: false,
      form: {
        type: "",
        type1:'',
        type2:'',
        name:''
      },
      form1: {
        type1:'',
      },
      form2: {
        type2:'',
      },
      rules1:{
        type1: [
          { required: true, validator: type1, trigger: "blur" },
        ],
        sType1: [
          { required: true,validator: sType1 },
        ],
      },
      rules2:{
        type2: [
          { required: true, validator: type2, trigger: "blur" },
        ],
      },
      rules: {
        type: [
          { required: true, validator: type, trigger: "blur" },
        ],
        type1: [
          { required: true, validator: type1, trigger: "blur" },
        ],
        // type2: [
        //   { required: true, validator: type2, trigger: "blur" },
        // ],
        sType: [
          { required: true,validator: sType },
        ],
        // sType1: [
        //   { required: true,validator: sType1 },
        // ],
      },
      id:'',
      id1:'',
      id2:''
    };
  },
  created() {
    this.getMaterialList()
    this.getSealingList()
    this.getSecondaryList()
  },
  methods: {
    handleQuery(){
       this.pages={
        pageNum:1,
        pageSize:10
      },
      this.getMaterialList()
    },
    handleQuery1(){
       this.pages1={
        pageNum:1,
        pageSize:10
      },
      this.getSealingList()
    },
    handleQuery2(){
       this.pages2={
        pageNum:1,
        pageSize:10
      },
      this.getSecondaryList()
    },
    getMaterialList(){
       let data ={
        condition:{
          packing_material_name:this.queryForm.name
        },
        pager:{
          limit:this.pages.pageSize,
          skip:this.pages.pageNum
        }
      }
      materialList(data).then(res=>{
        if(res.code==200){
          console.log(res.data)
          this.data = res.data.data
          this.total = res.data.total
        }
      })
    },
    getSealingList(){
       let data ={
        condition:{
          sealing_name:this.queryForm.name1
        },
        pager:{
          limit:this.pages1.pageSize,
          skip:this.pages1.pageNum
        }
      }
      sealingList(data).then(res=>{
        if(res.code==200){
          console.log(this.data1)
          this.data1 = res.data.data
          this.total1 = res.data.total
        }
      })
    },
    getSecondaryList(){
      let data ={
        condition:{
          secondary_measure_name:this.queryForm.name2
        },
        pager:{
          limit:this.pages2.pageSize,
          skip:this.pages2.pageNum
        }
      }
      secondaryList(data).then(res=>{
        if(res.code==200){
          console.log(this.data1)
          this.data2 = res.data.data
          this.total2 = res.data.total
        }
      })
    },
    addType() {
        this.open = true
        this.title = "添加包装材料"
        this.getMaterialList()
        this.getSealingList()
        this.form={
          type2:'',
          type1: "",
          type: "",
          name:''
        }
    },
    editType(row){
        this.open = true
        this.title="编辑包装材料"
        console.log(row)
        this.form.type = row.packing_material_name
        this.id = row.id
        this.checkList = row.packing_material_type
    },
    deleteItem(row,index){
       this.$confirm("是否确认删除 " + row.packing_material_name	 + " ?", "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
        .then(() => {
            materialDelete(row.id).then(res => {
              if(res.code==200){
                this.$message.success('删除成功')
                this.getMaterialList()
              }else{
                this.$message.error(res.msg)
              }
            });
        })
        .catch(function() {});
    },
    addType1() {
        this.open1 = true
        this.title1 = "添加封口形式"
        this.getMaterialList()
        this.getSealingList()
        this.form1={
          // type2:'',
          type1: "",
          // type: "",
          // name:''
        }
    },
    editType1(row){
        this.open1 = true
        this.title1="编辑封口形式"
        this.form1.type1 = row.sealing_name
        this.id1 = row.id
        this.checkList1 = row.sealing_type
    },
    deleteItem1(row,index){
       this.$confirm("是否确认删除 " + row.sealing_name	 + " ?", "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
        .then(() => {
            sealingDelete(row.id).then(res => {
              if(res.code==200){
                this.$message.success('删除成功')
                this.getSealingList()
              }else{
                this.$message.error(res.msg)
              }
            });
        })
        .catch(function() {});
    },
    addType2() {
        this.open2 = true
        this.title2 = "添加辅助措施"
        this.getMaterialList()
        this.getSealingList()
        this.form={
          type2:'',
          type1: "",
          type: "",
          name:''
        }
        this.form2={
          type2:'',
        }
        // this.$nextTick(() => {
        //   this.$refs.form.clearValidate('sType')
        //   this.$refs.form.clearValidate('sType1')
        // })
    },
    editType2(row){
        this.open2 = true
        this.title2="编辑辅助措施"
        this.form2.type2 = row.secondary_measure_name
        this.id2 = row.id
    },
    deleteItem2(row,index){
       this.$confirm("是否确认删除 " + row.secondary_measure_name + " ?", "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
        .then(() => {
            secondaryDelete(row.id).then(res => {
              if(res.code==200){
                this.$message.success('删除成功')
                this.getSecondaryList()
              }else{
                this.$message.error(res.msg)
              }
            });
        })
        .catch(function() {});
    },
    afreshList(){
      this.getMaterialList()
    },
    afreshList1(){
      this.getSealingList()
    },
    afreshList2(){
      this.getSecondaryList()
    },
    handleClose() {
        this.open = false
        this.getMaterialList()
        this.getSealingList()
        this.form={
          type2:'',
          type1: "",
          type: "",
          name:''
        },
        this.form2={
          type2:'',
        },
        this.checkList=[]
        this.checkList1=[]
        this.$nextTick(() => {
          this.$refs.form.clearValidate('sType')
          // this.$refs.form.clearValidate('sType1')
        })
        this.open1 = false
        this.open2 = false
    },
    handleClose1() {
        this.open = false
        this.getMaterialList()
        this.getSealingList()
        this.form1={
          type1: "",
        },
        this.checkList=[]
        this.checkList1=[]
        this.$nextTick(() => {
          this.$refs.form1.clearValidate('sType1')
        })
        this.open1 = false
        this.open2 = false
    },
    handleClose2() {
        this.open = false
        this.getMaterialList()
        this.getSealingList()
        this.form={
          type2:'',
          type1: "",
          type: "",
          name:''
        },
        this.form2={
          type2:'',
        },
        this.checkList=[]
        this.checkList1=[]
        this.open1 = false
        this.open2 = false
    },
    cSubmitForm(){
      this.$refs.form.validate((valid) => {
        if (valid) {
            if(this.title =='添加包装材料'){
             let data = {
              packing_material_name:this.form.type,
              packing_material_type:this.checkList
            }
            materialCreate(data).then(res=>{
              if(res.code==200){
                this.$message.success('添加成功')
                this.form.type=''
                this.checkList = []
                this.$nextTick(() => {
                  this.$refs.form.clearValidate('sType')
                  this.$refs.form.clearValidate('sType1')
                })
              }else{
                this.$message.error(res.msg)
              }
            })
          }
        }
      })
    },
    cSubmitForm1(){
      this.$refs.form1.validate((valid) => {
        if (valid) {
            if(this.title1 =='添加封口形式'){
             let data = {
              sealing_name:this.form1.type1,
              sealing_type:this.checkList1
            }
            sealingCreate(data).then(res=>{
              if(res.code==200){
                this.$message.success('添加成功')
                this.getSealingList()
                this.form1.type1 = ""
                this.checkList1 = []
                this.$nextTick(() => {
                  // this.$refs.form.clearValidate('sType')
                  this.$refs.form1.clearValidate('sType1')
                })
              }else{
                this.$message.error(res.msg)
              }
            })
          }
        }
      })
    },
    submitForm() {
        this.$refs.form.validate((valid) => {
        if (valid) {
            if(this.title =='添加包装材料'){
              let data = {
              packing_material_name:this.form.type,
              packing_material_type:this.checkList
            }
            materialCreate(data).then(res=>{
              if(res.code==200){
                this.$message.success('添加成功')
                this.getMaterialList()
                this.open = false
                this.checkList=[]
                this.checkList1=[]
                this.$nextTick(() => {
                  this.$refs.form.clearValidate('sType')
                  this.$refs.form.clearValidate('sType1')
                })
              }else{
                this.$message.error(res.msg)
              }
            })
          }else{
            let data = {
              packing_material_name:this.form.type,
              packing_material_type:this.checkList
            }
            materialUpdate(this.id,data).then(res=>{
              if(res.code==200){
                this.$message.success('编辑成功')
                this.getMaterialList()
                this.open = false
                this.checkList=[]
                this.checkList1=[]
                this.$nextTick(() => {
                  this.$refs.form.clearValidate('sType')
                  // this.$refs.form.clearValidate('sType1')
                })
              }else{
                this.$message.error(res.msg)
              }
            })
          }
        }
      })
    },
    submitForm1() {
        this.$refs.form1.validate((valid) => {
        if (valid) {
            if(this.title1 =='添加封口形式'){
              let data = {
              sealing_name:this.form1.type1,
              sealing_type:this.checkList1
            }
            sealingCreate(data).then(res=>{
              if(res.code==200){
                this.$message.success('添加成功')
                this.getSealingList()
                this.open1 = false
                this.checkList=[]
                this.checkList1=[]
                this.$nextTick(() => {
                  // this.$refs.form.clearValidate('sType')
                  this.$refs.form1.clearValidate('sType1')
                })
              }else{
                this.$message.error(res.msg)
              }
            })
          }else{
            let data = {
              sealing_name:this.form1.type1,
              sealing_type:this.checkList1
            }
            sealingUpdate(this.id1,data).then(res=>{
              if(res.code==200){
                this.$message.success('编辑成功')
                this.getSealingList()
                this.open1 = false
                this.checkList=[]
                this.checkList1=[]
                this.$nextTick(() => {
                  // this.$refs.form1.clearValidate('sType')
                  this.$refs.form.clearValidate('sType1')
                })
              }else{
                this.$message.error(res.msg)
              }
            })
          }
        }
      })
    },
    cSubmitForm2(){
     this.$refs.form2.validate((valid) => {
        if (valid) {
            let data = {
              secondary_measure_name:this.form2.type2,
            }
            secondaryCreate(data).then(res=>{
              if(res.code==200){
                this.$message.success('添加成功')
                this.getSecondaryList()
                this.form2.type2=''
              }else{
                this.$message.error(res.msg)
              }
            })
        }
     })
    },
    submitForm2() {
        this.$refs.form2.validate((valid) => {
        if (valid) {
            if(this.title2 =='添加辅助措施'){
              let data = {
              secondary_measure_name:this.form2.type2,
            }
            secondaryCreate(data).then(res=>{
              if(res.code==200){
                this.$message.success('添加成功')
                this.getSecondaryList()
                this.open2 = false
                // this.$nextTick(() => {
                //   this.$refs.form.clearValidate('sType')
                //   this.$refs.form.clearValidate('sType1')
                // })
              }else{
                this.$message.error(res.msg)
              }
            })
          }else{
            let data = {
              secondary_measure_name:this.form2.type2,
            }
            secondaryUpdate(this.id2,data).then(res=>{
              if(res.code==200){
                this.$message.success('编辑成功')
                this.getSecondaryList()
                this.open2 = false
                // this.$nextTick(() => {
                //   this.$refs.form.clearValidate('sType')
                //   this.$refs.form.clearValidate('sType1')
                // })
              }else{
                this.$message.error(res.msg)
              }
            })
          }
        }
      })
    },
    handleClick() {
      this.form={
          type2:'',
          type1: "",
          type: "",
          name:''
        }
        this.form2={
          type2:'',
        }
    },
  },
};
</script>
<style lang="scss">
</style>